// Problem: Trie字符串统计
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/837/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
using namespace std;

const int N=100010;

int son[N][26],cnt[N],idx;
char str[N];

void insert(char str[])
{
	int p=0;
	for(int i=0;str[i];++i)
	{
		int u=str[i]-'a';
		if(!son[p][u]) son[p][u]=++idx;
		p=son[p][u];
	}
	++cnt[p];
}

int query(char str[])
{
	int p=0;
	for(int i=0;str[i];++i)
	{
		int u=str[i]-'a';
		if(!son[p][u]) return 0;
		p=son[p][u];
	}
	return cnt[p];
}
int main()
{
	int n;scanf("%d",&n);
	while(n--)
	{
		char ch[2];
		scanf("%s %s",ch,str);
		if(ch[0]=='I')
		{
			
			insert(str);
		}
		else
		{
			printf("%d\n",query(str));
		}
	}
	return 0;
}